package com.bluemson.service.Impl;

import com.bluemson.dao.ManagerDao;
import com.bluemson.dao.UserDao;
import com.bluemson.entity.Article;
import com.bluemson.entity.Plate;
import com.bluemson.service.UpdateService;
import com.bluemson.util.MyBatisUtil;
import com.bluemson.util.TimeUtil;
import org.apache.ibatis.session.SqlSession;

public class UpdateServiceImpl implements UpdateService {
    private final SqlSession session = MyBatisUtil.getSqlSession();
    ManagerDao managerDao = session.getMapper(ManagerDao.class);
    @Override
    public boolean updatePlate(Plate plate){
        boolean flag=false;
        if(managerDao.checkPlate(plate).isEmpty()){
            managerDao.updatePlate(plate);
            session.commit();
            flag=true;
        }
        return flag;
    }
    @Override
    public void moveArticle(Article article){
        managerDao.moveArticle(article);
    }
    @Override
    public void topArticle(int id,int top){
        if(top == 1){
            managerDao.topArticle(id);
            session.commit();
        }else {
            managerDao.noTopArticle(id);
            session.commit();
        }
    }
    @Override
    public void like(Article article){
        UserDao userDao = session.getMapper(UserDao.class);
        userDao.like(article.getId());
        session.commit();
        article.setTime(TimeUtil.newDateTime());
        userDao.likeArticle(article);
        session.commit();
    }
}
